import datetime, time
from appium import webdriver

desired_caps = {
        "platformName": "Android",  # 操作系统
        "deviceName": "emulator-5554",  # 设备 ID
        "platformVersion": "6.0.1",  # 设备版本号
        "appPackage": "com.tencent.mp",  # app 包名
        "appActivity": "com.tencent.mp.feature.launcher.ui.LauncherActivity",  # app 启动时主 Activity
        'noReset': True,  # 是否保留 session 信息，可以避免重新登录
        'unicodeKeyboard': True,  # 使用 unicodeKeyboard 的编码方式来发送字符串
        'resetKeyboard': True  # 将键盘给隐藏起来
    }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)


def post(title, content):
        time.sleep(9)
        # 发表
        driver.find_element_by_xpath('//android.widget.FrameLayout[@content-desc="发表"]/android.widget.ImageView').click()
        time.sleep(2)
        # 文章
        driver.find_element_by_id(
                'com.tencent.mp:id/tv_article').click()
        time.sleep(2)
        # 标题
        driver.find_element_by_xpath(
               '//android.view.View[@content-desc="标题"]').send_keys(title)
        time.sleep(2)
        # 想说点什么
        driver.find_element_by_xpath(
                '//android.view.View[@content-desc="想说点什么"]').send_keys(content)
        time.sleep(2)
        # 下一步
        driver.tap([(710, 70), (770, 91)], 300)
        time.sleep(5)

        # 添加封面
        driver.tap([(310, 251), (405, 307)], 300)
        time.sleep(1)
        driver.tap([(141,1388), (191,1401)], 300)
        time.sleep(1)
        driver.find_element_by_id(
                'com.tencent.mp:id/iv_photo').click()
        time.sleep(1)
        driver.find_element_by_id(
                'com.tencent.mp:id/action_option_btn').click()
        time.sleep(10)

        # 发表
        driver.find_element_by_id(
                'com.tencent.mp:id/action_option_btn').click()
        time.sleep(1)
        driver.find_element_by_id(
                'com.tencent.mp:id/mm_alert_ok_btn').click()
        time.sleep(1)


if __name__ == "__main__":
        post("阶段性测试title", "今天是"+str(datetime.date.today()))
        driver.quit()
